const express = require('express');
const router = express.Router();
const chinaTime = require('china-time');
const bodyParser = require('body-parser');
const userSQL = require('../../db/userSQL')
const loginSQL = require('../../db/loginSQL')

router.post('/setInfo', bodyParser.json(), (req, res) => {
    // 检查用户ID是否为空
    if (!req.body["userID"]
        || typeof req.body["userID"] != "number") {
        res.json({
            err: 1,
            message: '用户ID为空'
        })
    } else if (!req.body["userName"]
        || typeof req.body["userName"] != "string"
        || !req.body["userName"].length) {
        res.json({
            err: 1,
            message: '用户名为空'
        })
    } else if (!req.body["userGender"]
        || typeof req.body["userGender"] != "string"
        || !req.body["userGender"].length) {
        res.json({
            err: 1,
            message: '用户性别为空'
        })
    } else if (!req.body["userAge"]
        || typeof req.body["userAge"] != "number") {
        res.json({
            err: 1,
            message: '用户年龄为空'
        })
    } else if (!req.body["userProfile"]
        || typeof req.body["userProfile"] != "string"
        || !req.body["userProfile"].length) {
        res.json({
            err: 1,
            message: '用户简介为空'
        })
    } else {
        let userID = req.body["userID"];
        let userName = req.body["userName"];
        let userGender = req.body["userGender"];
        let userAge = req.body["userAge"];
        let userProfile = req.body["userProfile"];
        // 下方改数据库操作代码，sql接口为userSQL.setinfo
        loginSQL.getByID(userID, (result) => {
            var userStatus = result[0].userStatus
            if (userStatus == 0) {
                res.json({
                    err: 3,
                    message: '用户被封禁'
                })
            }
            else {
                userSQL.setinfo(userID, userName, userGender, userAge, userProfile, (result) => {
                    userSQL.getinfo(userID, (result) => {
                        var user = result[0]
                        user.err = 200
                        console.log(user)
                        res.json(user)
                    })
                })
            }
        })
    }
})

module.exports = router;